

#include "add.h"
#include "app_config.h"
#ifndef APPCFG_DEFAULT_MQTT_PORT
#  define APPCFG_DEFAULT_MQTT_PORT 1883
#endif


void publish_data(const char *broker, const char *topic, const char *data) {
    mosquitto_lib_init();
    struct mosquitto *mosq = mosquitto_new(NULL, true, NULL);
    if (mosq == NULL) {
        printf("Error: Out of memory.\n");
        return;
    }

    int error = mosquitto_connect(mosq, broker, APPCFG_DEFAULT_MQTT_PORT, 60);
    if (error != MOSQ_ERR_SUCCESS) {
        printf("Error: %s\n", mosquitto_strerror(error));
        mosquitto_destroy(mosq);
        return;
    }

    error = mosquitto_publish(mosq, NULL, topic, strlen(data), data, 2, false);
    if (error != MOSQ_ERR_SUCCESS) {
        printf("Error: %s\n", mosquitto_strerror(error));
    }

    //mosquitto_destroy(mosq);
    //mosquitto_lib_cleanup();
}
